package org.springblade.modules.resource.controller;

import com.alibaba.fastjson.JSONObject;
import com.iflytek.sc.ssp.uc.client.constant.UCConstant;
import com.iflytek.sc.ssp.uc.client.context.UCCasServiceContext;
import com.iflytek.sc.ssp.uc.client.service.UCService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.resource.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/api/cas")
public class CasLoginController {

	@Value("${uc-config.redirectUrl}")
	private String redirectUrl;

	@GetMapping("/uc/login")
	public void ticket(HttpServletResponse response, HttpServletRequest request) throws IOException {
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			//String id = request.getSession(true).getId();
			//System.out.println("id===="+id);
			//System.out.println("||||||||||||||||||||||||登录=========="+cookie.getName()+"===="+cookie.getValue()+"==="+cookie.getPath());
			Cookie sessionCookie = new Cookie(cookie.getName(), cookie.getValue());
			sessionCookie.setMaxAge(60 * 60);
			sessionCookie.setPath("/");
			response.addCookie(sessionCookie);
		}

		UCService ucservice = UCCasServiceContext.getUcService();
		String token = ucservice.getCurrentToken(request.getSession());
		System.out.println("token=" + token);
		Cookie cookie = new Cookie("token", token);
		cookie.setMaxAge(60 * 60);
		cookie.setPath("/");
		response.addCookie(cookie);
		response.sendRedirect(redirectUrl);
	}


	@ApiOperation(value = "单点登出")
	@GetMapping("/logout")
	@ResponseBody
	public void userLogout(HttpServletResponse response, HttpServletRequest request) {
		String url = "/v2/user/logout";
		String logout = "https://sso.ahzwfw.gov.cn/uccp-server/logout?page=" + redirectUrl;
		try {
			UCService ucservice = UCCasServiceContext.getUcService();
			String token = ucservice.getCurrentToken(request.getSession(true));
			//System.out.println(token);
			Cookie[] cookies = request.getCookies();
			for (Cookie cookie : cookies) {
				//System.out.println("||||||||||||||||||||||||退出======="+cookie.getName()+"===="+cookie.getValue()+"==="+cookie.getPath());
				cookie.setMaxAge(0);
				cookie.setPath("/");
				response.addCookie(cookie);
			}
			//String id = request.getSession(true).getId();
			//System.out.println("id====="+id);
			request.getSession(true).invalidate();
			Cookie cookie = new Cookie("token", token);
			cookie.setMaxAge(0);
			cookie.setPath("/");
			response.addCookie(cookie);
			HttpUtil.postHandle(url, token);
			response.sendRedirect(logout);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
